//
//  RWAddressTableViewCell.m
//  RedWine
//
//  Created by Liufangfang on 2017/1/14.
//  Copyright © 2017年 HuiBangKe. All rights reserved.
//

#import "RWAddressTableViewCell.h"
#import "RWAddressModel.h"


@interface RWAddressTableViewCell ()

@property (nonatomic, copy) RWAddressModel *addressModel;

@end

@implementation RWAddressTableViewCell

#pragma mark - Init Methods

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setAddressUI];
    }
    
    return self;
}

#pragma mark - Load Datas

- (void)loadDataWithModel:(id)model{
    
    _addressModel = [RWAddressModel mj_objectWithKeyValues:model];
    [self configDatas];
}

- (void)configDatas{
 
    _receivePersonLab.text = [NSString stringWithFormat:@"收件人：  %@  %@", _addressModel.true_name, _addressModel.mob_phone];
    _addrLab.text = [NSString stringWithFormat:@"%@ %@", _addressModel.area_info, _addressModel.address];
    if (_addressModel.is_default == 0) {
        _defaultBtn.selected = NO;
    }else if(_addressModel.is_default == 1){
        _defaultBtn.selected = YES;
    }
}

#pragma mark - Config UI

- (void)setAddressUI{
    
    UILabel *receivePersonLab = [[UILabel alloc] init];
    [self.contentView addSubview:receivePersonLab];
    receivePersonLab.textColor = [UIColor blackColor];
    receivePersonLab.font = FTY_FONTTSIZE(15);
    [receivePersonLab sizeToFit];
    [receivePersonLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(10.*FTY_WIDTH_BASE);
        make.right.mas_equalTo(-10.*FTY_WIDTH_BASE);
    }];
    _receivePersonLab = receivePersonLab;
    
    UILabel *addrLab = [[UILabel alloc] init];
    [self.contentView addSubview:addrLab];
    addrLab.textColor = [UIColor blackColor];
    addrLab.font = FTY_FONTTSIZE(15);
    [addrLab sizeToFit];
    [addrLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_receivePersonLab.mas_bottom).offset(10.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(_receivePersonLab.mas_left);
        make.right.mas_equalTo(-10.*FTY_WIDTH_BASE);
    }];
    _addrLab = addrLab;
    
    //中间线
    UIView *midLine = [[UIView alloc] init];
    [self.contentView addSubview:midLine];
    midLine.backgroundColor = [FTYColor grayColor];
    [midLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_addrLab.mas_bottom).offset(10.*FTY_HEIGHT_BASE);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(.5*FTY_HEIGHT_BASE);
    }];
    
    UIButton *defaultBtn = [[UIButton alloc] init];
    [self.contentView addSubview:defaultBtn];
    [defaultBtn setImage:[UIImage imageNamed:@"noselected_icon"] forState:UIControlStateNormal];
    [defaultBtn setImage:[UIImage imageNamed:@"selected_icon"] forState:UIControlStateSelected];
    [defaultBtn addTarget:self action:@selector(defaultBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [defaultBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(midLine.mas_bottom).offset(5.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(_receivePersonLab.mas_left);
        make.width.mas_equalTo(20.*FTY_WIDTH_BASE);
        make.height.mas_equalTo(20.*FTY_WIDTH_BASE);
        //make.width.mas_equalTo(25);
        //make.height.mas_equalTo(25);
    }];
    _defaultBtn = defaultBtn;
    //_defaultBtn.backgroundColor = [UIColor blueColor];
    //_defaultBtn.imageEdgeInsets = UIEdgeInsetsMake(25, 25, 25, 25);
    
    UILabel *defaultLab = [[UILabel alloc] init];
    [self.contentView addSubview:defaultLab];
    defaultLab.text = @"设为默认地址";
    defaultLab.textColor = [UIColor blackColor];
    defaultLab.font = FTY_FONTTSIZE(15);
    [defaultLab sizeToFit];
    [defaultLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(midLine.mas_bottom).offset(6.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(defaultBtn.mas_right).offset(10.*FTY_WIDTH_BASE);
        make.right.mas_equalTo(-10.*FTY_WIDTH_BASE);
    }];
    
    UIButton *deleteBtn = [[UIButton alloc] init];
    [self.contentView addSubview:deleteBtn];
    [deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
    [deleteBtn setTitleColor:[FTYColor grayColor] forState:UIControlStateNormal];
    deleteBtn.titleLabel.font = FTY_FONTTSIZE(15);
    [deleteBtn addTarget:self action:@selector(deleteBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(defaultLab.mas_top);
        make.right.mas_equalTo(-10.*FTY_WIDTH_BASE);
        make.height.mas_equalTo(20.*FTY_HEIGHT_BASE);
        make.width.mas_equalTo(60.*FTY_WIDTH_BASE);
    }];
    
    UIButton *editBtn = [[UIButton alloc] init];
    [self.contentView addSubview:editBtn];
    [editBtn setTitle:@"编辑" forState:UIControlStateNormal];
    [editBtn setTitleColor:[FTYColor grayColor] forState:UIControlStateNormal];
    editBtn.titleLabel.font = FTY_FONTTSIZE(15);
    [editBtn addTarget:self action:@selector(editBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.height.width.mas_equalTo(deleteBtn);
        make.right.mas_equalTo(deleteBtn.mas_left).offset(10.*FTY_WIDTH_BASE);
    }];
    
    //底部线
    UIView *bottomLine = [[UIView alloc] init];
    [self.contentView addSubview:bottomLine];
    bottomLine.backgroundColor = [FTYColor grayColor];
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(.5*FTY_HEIGHT_BASE);
    }];
}

#pragma mark - Private Invoke Method

- (void)defaultBtnAction:(UIButton *)btn{

    btn.selected = !btn.selected;
    
    if ([_delegate respondsToSelector:@selector(defaultAddress:atIndex:)]) {
        [_delegate defaultAddress:btn atIndex:self.tag];
    }
}

- (void)deleteBtnAction:(UIButton *)btn{

    if ([_delegate respondsToSelector:@selector(deleteAddress:atIndex:)]) {
        [_delegate deleteAddress:btn atIndex:self.tag];
    }
}

- (void)editBtnAction:(UIButton *)btn{

    if ([_delegate respondsToSelector:@selector(editAddress:atIndex:)]) {
        [_delegate editAddress:btn atIndex:self.tag];
    }
}

@end
